﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SQLite;

namespace MOJsdk
{
    public class ISO639_1
    {
        [PrimaryKey, AutoIncrement]
        public int Index { get; set; }
        [Unique]
        public string Code { get; set; }
        [Unique]
        public string Country { get; set; }

        [Ignore]
        public string StrIso
        { get { return string.Format("{0} ({1})", Country, Code); } }

        public static List<ISO639_1> GetLanguages()
        {
            List<ISO639_1> list = new List<ISO639_1>();

            list.Add(new ISO639_1() { Code = "xx", Country = "Other" });
            list.Add(new ISO639_1() { Code = "af", Country = "Afrikaans" });
            list.Add(new ISO639_1() { Code = "ar", Country = "Arabic" });
            list.Add(new ISO639_1() { Code = "be", Country = "Belarusian" });
            list.Add(new ISO639_1() { Code = "bg", Country = "Bulgarian" });
            list.Add(new ISO639_1() { Code = "my", Country = "Burmese" });
            list.Add(new ISO639_1() { Code = "ca", Country = "Catalan" });
            list.Add(new ISO639_1() { Code = "cs", Country = "Czech" });
            list.Add(new ISO639_1() { Code = "cy", Country = "Welsh" });
            list.Add(new ISO639_1() { Code = "da", Country = "Danish" });
            list.Add(new ISO639_1() { Code = "de", Country = "German" });
            list.Add(new ISO639_1() { Code = "dv", Country = "Divehi" });
            list.Add(new ISO639_1() { Code = "el", Country = "Greek" });
            list.Add(new ISO639_1() { Code = "en", Country = "English" });
            list.Add(new ISO639_1() { Code = "es", Country = "Spanish" });
            list.Add(new ISO639_1() { Code = "et", Country = "Estonian" });
            list.Add(new ISO639_1() { Code = "fa", Country = "Persian" });
            list.Add(new ISO639_1() { Code = "fi", Country = "Finnish" });
            list.Add(new ISO639_1() { Code = "fr", Country = "French" });
            list.Add(new ISO639_1() { Code = "ga", Country = "Irish" });
            list.Add(new ISO639_1() { Code = "gu", Country = "Gujarati" });
            list.Add(new ISO639_1() { Code = "he", Country = "Hebrew" });
            list.Add(new ISO639_1() { Code = "hi", Country = "Hindi" });
            list.Add(new ISO639_1() { Code = "hr", Country = "Croatian" });
            list.Add(new ISO639_1() { Code = "hu", Country = "Hungarian" });
            list.Add(new ISO639_1() { Code = "hy", Country = "Armenian" });
            list.Add(new ISO639_1() { Code = "is", Country = "Icelandic" });
            list.Add(new ISO639_1() { Code = "iu", Country = "Inuktitut" });
            list.Add(new ISO639_1() { Code = "it", Country = "Italian" });
            list.Add(new ISO639_1() { Code = "ja", Country = "Japanese" });
            list.Add(new ISO639_1() { Code = "ka", Country = "Georgian" });
            list.Add(new ISO639_1() { Code = "kn", Country = "Kannada" });
            list.Add(new ISO639_1() { Code = "ko", Country = "Korean" });
            list.Add(new ISO639_1() { Code = "kj", Country = "Kuanyama" });
            list.Add(new ISO639_1() { Code = "ku", Country = "Kurdish" });
            list.Add(new ISO639_1() { Code = "lo", Country = "Laothian" });
            list.Add(new ISO639_1() { Code = "lt", Country = "Lithuanian" });
            list.Add(new ISO639_1() { Code = "lv", Country = "Latvian" });
            list.Add(new ISO639_1() { Code = "mk", Country = "Macedonian" });
            list.Add(new ISO639_1() { Code = "ml", Country = "Malayalam" });
            list.Add(new ISO639_1() { Code = "ms", Country = "Malay" });
            list.Add(new ISO639_1() { Code = "no", Country = "Norwegian" });
            list.Add(new ISO639_1() { Code = "nl", Country = "Dutch" });
            list.Add(new ISO639_1() { Code = "or", Country = "Oriya" });
            list.Add(new ISO639_1() { Code = "pa", Country = "Punjabi" });
            list.Add(new ISO639_1() { Code = "pl", Country = "Polish" });
            list.Add(new ISO639_1() { Code = "pt", Country = "Portuguese" });
            list.Add(new ISO639_1() { Code = "ro", Country = "Romanian" });
            list.Add(new ISO639_1() { Code = "ru", Country = "Russian" });
            list.Add(new ISO639_1() { Code = "si", Country = "Sinhalese" });
            list.Add(new ISO639_1() { Code = "sk", Country = "Slovak" });
            list.Add(new ISO639_1() { Code = "sl", Country = "Slovenian" });
            list.Add(new ISO639_1() { Code = "sr", Country = "Serbian" });
            list.Add(new ISO639_1() { Code = "sw", Country = "Swahili" });
            list.Add(new ISO639_1() { Code = "sv", Country = "Swedish" });
            list.Add(new ISO639_1() { Code = "ta", Country = "Tamil" });
            list.Add(new ISO639_1() { Code = "te", Country = "Telugu" });
            list.Add(new ISO639_1() { Code = "th", Country = "Thai" });
            list.Add(new ISO639_1() { Code = "tr", Country = "Turkish" });
            list.Add(new ISO639_1() { Code = "uk", Country = "Ukrainian" });
            list.Add(new ISO639_1() { Code = "vi", Country = "Vietnamese" });
            list.Add(new ISO639_1() { Code = "yi", Country = "Yiddish" });

            return list;
        }
        public static List<ISO639_1> LoadLanguages()
        {
            using (SQLiteConnection _cn = new SQLiteConnection(GlobalVars.TheHost.SystemVars.DataBaseLocal))
            {
                return _cn.Table<ISO639_1>().ToList();
            }
        }
        public static ISO639_1 LoadLanguage(int INDEX)
        {
            ISO639_1 lang = null;

            try
            {
                using (SQLiteConnection _cn = new SQLiteConnection(GlobalVars.TheHost.SystemVars.DataBaseLocal))
                {
                    lang = _cn.Table<ISO639_1>().First(x => x.Index == INDEX);
                }

                return lang;
            }
            catch (Exception ex)
            {
                throw new IndexOutOfRangeException("Invalid Index", ex);
            }
        }
        public static int GetLanguageIndex(string Code)
        {
            Code = Code.ToLower();
            using (SQLiteConnection _cn = new SQLiteConnection(GlobalVars.TheHost.SystemVars.DataBaseLocal))
            {
                List<ISO639_1> list = new List<ISO639_1>(_cn.Table<ISO639_1>().ToList());
                foreach(var v in list)
                {
                    if (Code == v.Code)
                        return v.Index;
                }
            }

            return 1;
        }
    }
}
